import pymysql
import pandas as pd
from Database import DatabaseHandler

db_handler = DatabaseHandler(host='localhost', port=3306, user='root', password='123456')
db_handler.create_database('test')
db_handler.cursor.execute('USE test')

db_handler.cursor3.execute('use test')
db_handler.cursor3.execute('create table if not exists School_introduction(\
                                        school_name varchar(225) primary key,\
                                        school_introduction mediumtext,\
                                        Gets_the_address text)')

df = pd.read_excel(r"C:\Users\姚宇康\Desktop\日常文件\学习\学校简介(1).xlsx")
df.fillna('', inplace=True)  # 填充空值

sql_insert = 'INSERT INTO School_introduction (school_name, school_introduction, Gets_the_address) VALUES (%s, %s, %s)'
try:
    db_handler.cursor3.executemany(sql_insert, df.to_records(index=False).tolist())
    print(db_handler.cursor3.rowcount, "记录插入成功。")
except pymysql.MySQLError as e:
    print(f"数据库错误：{e}")
    # 注意：由于autocommit=True，通常不需要手动回滚

db_handler.close_connection()